<?php

class SiteAction extends SiteBaseAction
{
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 网站列表
     */
    public function index()
    {
        $site = Session('site');
        //网站主ID
        $user_id = $site['user_id'];

        //分页每页的条数
        $perPage = $this->getPerPage();

        $map['user_id'] = $user_id;
        if (I('get.site_name')) {
            $site_name = I('get.site_name');
            $map['site_name'] = array('like', '%'.$site_name.'%');
        }

        $currPage = intval($_GET['p']);//当前页数
        $currPage < 1 && $currPage = 1;

        $itemCount = M('Site')->where($map)->order('site_id desc')->count();

        $itemPerPage = $perPage;//每页的条数
        $pageUrl = $this->baseUrl . '/Medium/Site/index';//url
        $paramQuery = array('site_name'=>$site_name);//参数
        $paginator = new Paginator($currPage, $itemCount, $itemPerPage, $pageUrl, $paramQuery);

        $sites = M('Site')->where($map)->order('site_id desc')->limit(($currPage-1)*$perPage, $perPage)->select();

        $page = $paginator->getHtml(1); //获取样式为1的分页HTML结构

        $this->assign('baseUrl', $this->baseUrl);
        $this->assign('manage_types',getManageType());
        $this->assign('site_name', $site_name);
        $this->assign('page', $page);
        $this->assign('sites',$sites);
        $this->display();
    }

    /**
     * 试图：添加网站
     */
    public function add()
    {
        $this->assign('baseUrl', $this->baseUrl);
        $this->assign('manage_types',getManageType());
        $this->display();
    }

    /**
     * 动作：添加网站
     */
    public function store()
    {
        //登录用户信息
        $site = Session('site');
        //网站主ID
        $user_id = $site['user_id'];

        $data = array();

        $data['site_name']              = I('post.site_name');//网站名称
        $data['virtual_currency']       = I('post.virtual_currency');//虚拟货币名称
        $data['safe_code']              = I('post.safe_code');//安全码
        $data['rate']                   = I('post.rate');//默认汇率
        $data['auto_link']             = I('post.auto_link');//自动返还虚拟货币链接
        $data['manually_link']          = I('post.manually_link');//手动返还虚拟货币链接
        $data['manage_type']            = I('post.manage_type');//网站经营类型(1-平台自营 2-代理经营)(平台自营没有user_id，代理经营有user_id
        $data['create_time']            = time();

        //网站主信息
        $data['user_id'] = $user_id;//网站所属网站主

        if (M('Site')->add($data)) {
            alert('success','增加成功',$this->baseUrl.'/Medium/Site/index');
            //$this->success('增加成功');
        }
        alert('error','添加失败');
        //$this->error('失败');
    }

    /**
     * 视图：编辑
     */
    public function edit()
    {
        $site_id = I('get.id');
        $map['site_id'] = $site_id;
        $site = M('Site')->where($map)->find();
        $this->assign('manage_types',getManageType());
        $this->assign('site',$site);
        $this->display();
    }

    /**
     * 动作：编辑
     */
    public function update()
    {
        //登录用户信息
        $site = Session('site');
        //网站主ID
        $user_id = $site['user_id'];

        $site_id = I('post.site_id');
        $map['site_id'] = $site_id;
        $site = M('Site')->where($map)->find();
        if (!$site) {
            alert('error','该条信息不存在');
        }
        $data = array();

        $data['site_name']              = I('post.site_name');//网站名称
        $data['virtual_currency']       = I('post.virtual_currency');//虚拟货币名称
        $data['safe_code']              = I('post.safe_code');//安全码
        $data['rate']                   = I('post.rate');//默认汇率
        $data['auto_link']              = I('post.auto_link');//自动返还虚拟货币链接
        $data['manually_link']          = I('post.manually_link');//手动返还虚拟货币链接
        $data['manage_type']            = I('post.manage_type');//网站经营类型(1-平台自营 2-代理经营)(平台自营没有user_id，代理经营有user_id
        $data['update_time']            = time();
        //网站主信息
        $data['user_id'] = $user_id;//网站所属网站主

        if (M('Site')->where($map)->save($data)) {
            alert('success','编辑成功',$this->baseUrl.'/Medium/Site/index');
        }
        alert('error','编辑失败');
    }

    /**
     * 动作：删除
     */
    public function delete()
    {
        $id = I('get.id');
        $item =array(
            'ret'=>0,
            'msg'=>''
        );
        $map['site_id'] = $id;
        if (M('Site')->where($map)->find()) {
            M('Site')->where($map)->delete();
            echo json_encode($item);exit;
        }
        $item['ret'] = 1;
        $item['msg'] ='要删除的数据不存在';
        echo json_encode($item);exit;
    }

}


